<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\controller\v1;

use app\api\model\China as ChinaModel;
use think\Config;
use think\Db;
use think\Request;
use WeMini\Message;
use WeMini\Newtmpl;

class China extends BaseController
{
    public function getList()
    {
        return ChinaModel::getList();
    }

    public function getCity()
    {
        $file = Request::instance()->server('document_root') . DS . 'static' . DS . 'api' . DS . 'city.json';
        $json_string = file_get_contents($file);
        $data = json_decode($json_string, true);
        return $data['city'];
    }

    public function demo()
    {
        $config = Config::get('wechat');
        $mini = new Message($config);
        $miniprogram = [
            'appid'    => 'wx57b12ea86f944c61',
            'pagepath' => '/pages/index/index',
        ];
        $data = [
            'first' => [
                'value' => '恭喜你预约成功！',
                'color' => '#173177',
            ], // 订单号
            'keyword1' => [
                'value' => '捷安检车站',
                'color' => '#173177',
            ], // 订单号
            'keyword2' => [
                'value' => '2020年10月12日',
                'color' => '#173177',
            ], // 订单号
            'keyword3' => [
                'value' => '09:00',
                'color' => '#173177',
            ], // 订单号
            'keyword4' => [
                'value' => 'JA_1234567980123',
                'color' => '#173177',
            ], // 订单号
            'remark' => [
                'value' => '欢迎再次预约！',
                'color' => '#173177',
            ], // 订单号
        ];
        $mp_template_msg = [
            'appid'       => 'wx438bff6c08b8ea75',
            'template_id' => 'KJN_YRA-8RqCX9y7INK8w_2gTFkUYsLJwBqiBNhOUTo',
            'url'         => 'www.baidu.com',
            'miniprogram' => $miniprogram,
            'data'        => $data,
        ];
        $param = [
            'touser'          => 'o2ki15ERDblEGFT9RHAnAe3ssOvM',
            'mp_template_msg' => $mp_template_msg,
        ];
        return $mini->uniformSend($param);
        die;
        $sn = Db::name('order')->limit('100000')->order('order_id desc')->column('order_sn');
        $arr = [];
        foreach ($sn as $item) {
            $arr[] = substr($item, -6);
        }
        dump(count($arr));
        dump(count(array_unique($arr)));
        die;
        // return $arr;
        $mini = new Newtmpl($config);
        $data = [
            'character_string2' => ['value' => 'JA_1234567980123'], // 订单号
            'thing1'            => ['value' => '预约检车'], // 预约项目
            'car_number3'       => ['value' => '黑A12345'], // 车牌号
            'phone_number5'     => ['value' => '15845001111'], // 手机号
            'time4'             => ['value' => '2020年8月26日 10:10'], // 预约时间
        ];
        $param = [
            'touser'            => 'o2ki15ERDblEGFT9RHAnAe3ssOvM',
            'template_id'       => 't-L5DpqEifNH1bPKU4_qGxZeN9edGdBcMIPHoqYiKMk',
            'page'              => '/pages/order-detail/order-detail?id=' . 39879,
            'data'              => $data,
            'miniprogram_state' => 'trial',
            'lang'              => 'zh_CN',
        ];
        // return json_encode($param, JSON_UNESCAPED_UNICODE);
        return $mini->send($param);
        //        return json(['code'=>0, 'msg'=>'阿斯蒂芬', 'data'=>[]]);
        //        $data = json_encode(['lxdh'=>'13145678911']);
        //        $car_jiekou = new CarInterface(['action'=>'12C05','data'=>$data]);
        //        dump($car_jiekou->appointment());
    }
}